import axios from 'axios'
import qs from 'qs'
import store from '../redux/store'
const instance = axios.create({
  // 设置请求地址
  balseUrl: "http://localhost:3007",
  // 设置请求超时
  timeout: 5000
})


// 请求拦截器
instance.interceptors.request.use(req => {
  const { valid } = qs.parse(req.data)
  // 校验功能接口权限
  if (valid && req.method === 'post') {
    const { role: { rights } } = store.getState().userInfo[0]
    delete req.data.valid
    let url = req.url.split('?')[0].substring(8)
    if (rights.includes(url)) {
      return req
    } else {
      return alert('无操作权限')
    }
  } else {
    return req
  }
}, err => {
  console.log(err)
})
// 响应拦截器
instance.interceptors.response.use(res => {
  // console.log('响应内容', res);
  return res.data
}, err => {
  console.log(err)
})


export default instance
